package com.info.dpunified.web.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.info.dpunified.common.exception.DPUnifiedException;
import com.info.dpunified.dpgen.service.UserManagementService;

public class LoginController extends SimpleFormController {
	
	
    protected final Log logger = LogFactory.getLog(getClass());

	public LoginController(){

	}

	private UserManagementService userManagementService;
 
	/**
	 * @return the userManagementService
	 */
	public UserManagementService getUserManagementService() {
		return userManagementService;
	}

	/**
	 * @param userManagementService the userManagementService to set
	 */
	public void setUserManagementService(UserManagementService userManagementService) {
		this.userManagementService = userManagementService;
	}

	@Override
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception 
	{
 
        logger.info("starting here @ login with " + request.getParameter("txtLogin"));

        String login = request.getParameter("txtLoginId");
        String password = request.getParameter("txtPassword");
        
        boolean loginCheck = false;
        
		try
		{
			loginCheck = this.getUserManagementService().login(login, password);
		}
		catch(DPUnifiedException dpue)
		{
			dpue.printStackTrace();
			return new ModelAndView("/loginFailure","userName",login);
		}
		
		if(loginCheck)
		{
			return new ModelAndView("/welcome","userName",login);
		}
		else
		{
			return new ModelAndView("/loginFailure","userName",login);
		}
	}

}
